package com.ruan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruan.entity.Orders;
import com.ruan.entity.R;
import com.ruan.entity.ShoppingCart;
import com.ruan.query.OrdersQuery;
import com.ruan.service.OrdersService;
import com.ruan.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName:OrdersController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/9/14 13:08
 **/
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/addOrder")
    @PreAuthorize("hasAuthority('addOrder')")
    public R<String> addOrder(String skuOptions, String userId, Integer addressId, Integer spuId, int num){
        return ordersService.addOrder(skuOptions,userId,addressId,spuId,num);
    }

    @RequestMapping("/getOrderById")
    @PreAuthorize("hasAuthority('getOrderById')")
    public R<Orders> getOrderById(String id){
        return ordersService.getOrderById(id);
    }

    @PostMapping("/addShoppingCart")
    @PreAuthorize("hasAuthority('addShoppingCart')")
    public R<Void> addShoppingCart(String skuOptions, String userId, Integer addressId, Integer spuId, int num){
        return shoppingCartService.addShoppingCart(skuOptions,userId,addressId,spuId,num);
    }

    @RequestMapping("/getShoppingCartByUserId")
    @PreAuthorize("hasAuthority('getShoppingCartByUserId')")
    public R<List<ShoppingCart>> getShoppingCartByUserId(String userId){
        return shoppingCartService.getShoppingCartByUserId(userId);
    }

    @RequestMapping("/deleteShoppingCartById")
    @PreAuthorize("hasAuthority('deleteShoppingCartById')")
    public R<Void> deleteShoppingCartById(Integer id){
        return shoppingCartService.deleteShoppingCartById(id);
    }

    @RequestMapping("/editShoppingCart")
    @PreAuthorize("hasAuthority('editShoppingCart')")
    public R<Void> editShoppingCart(Integer id,Integer addressId,Integer num){
        return shoppingCartService.editShoppingCart(id,addressId,num);
    }

    @RequestMapping("/addShoppingCartOrder")
    @PreAuthorize("hasAuthority('addShoppingCartOrder')")
    public R<String> addShoppingCartOrder(Integer id){
        return shoppingCartService.addShoppingCartOrder(id);
    }

    @RequestMapping("/getOrdersListByUserId")
    @PreAuthorize("hasAuthority('getOrdersListByUserId')")
    public R<List<Orders>> getOrdersListByUserId(String userId){
        return ordersService.getOrdersListByUserId(userId);
    }

    @RequestMapping("/deleteOrdersById")
    @PreAuthorize("hasAuthority('deleteOrdersById')")
    public R<Void> deleteOrdersById(String id){
        return ordersService.deleteOrdersById(id);
    }

    @RequestMapping("/getAllOrders")
    @PreAuthorize("hasAuthority('getAllOrders')")
    public R<IPage<Orders>> getAllOrders(OrdersQuery ordersQuery){
        return ordersService.getAllOrders(ordersQuery);
    }

}
